package com.test.nnooolwdeaer.view_wooden.info_input_wooden;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.adapter_wooden.InfoInputListAdapter_wooden;
import com.test.nnooolwdeaer.bean_wooden.process_wooden.ProcessData_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.view_wooden.base_wooden.BaseInfoInputViewManager_wooden;

import java.util.ArrayList;
import java.util.Arrays;

public class SelectViewManager_wooden extends BaseInfoInputViewManager_wooden {

    private TextView titleTv_wooden;
    private ViewGroup clickLayout_wooden;
    private TextView inputTv_wooden;
    private ViewGroup listLayout_wooden;
    private ListView listLv_wooden;

    private int itemHeight_wooden = 0;
    private ArrayList<String> items_wooden;
    private InfoInputListAdapter_wooden listAdapter_wooden;

    private long startTime;

    public SelectViewManager_wooden(View view, ProcessData_wooden data,String trackWord){
        super(view,data,trackWord);
    }

    @Override
    protected void init_wooden(){

        titleTv_wooden = mView_wooden.findViewById(R.id.view_select_tv_title);
        clickLayout_wooden = mView_wooden.findViewById(R.id.view_select_layout_click);
        inputTv_wooden = mView_wooden.findViewById(R.id.view_select_tv_input);
        listLayout_wooden = mView_wooden.findViewById(R.id.view_select_layout_list);
        listLv_wooden = mView_wooden.findViewById(R.id.view_select_lv_list);

        setTitle_wooden(mData_wooden.getTitle_wooden());
        inputTv_wooden.setHint(StringUtil_wooden.getSafeString_wooden(mData_wooden.getHint_wooden()));

        clickLayout_wooden.setOnClickListener(v -> {
            if (listLayout_wooden.getVisibility()==VISIBLE)
                listLayout_wooden.setVisibility(GONE);
            else {
                startTime = System.currentTimeMillis();
                listLayout_wooden.setVisibility(VISIBLE);
            }
        });

        addItems_wooden(mData_wooden.getTitlesOfValue_wooden());
    }

    @Override
    public boolean checkInput_wooden() {
        if (!mData_wooden.isMustInput_wooden()) return true;
        return !TextUtils.isEmpty(inputTv_wooden.getText());
    }

    @Override
    public String getInput_wooden() {
        if (mData_wooden == null) return "";
        return mData_wooden.getValueOfTitle_wooden(StringUtil_wooden.getSafeString_wooden(inputTv_wooden.getText().toString()));
    }

    public String getTextInput_wooden(){
        return StringUtil_wooden.getSafeString_wooden(inputTv_wooden.getText().toString());
    }

    public void setTitle_wooden(String title){
        if (TextUtils.isEmpty(title)) titleTv_wooden.setVisibility(GONE);
        else {
            titleTv_wooden.setVisibility(VISIBLE);
            titleTv_wooden.setText(title);
        }
    }

    public void addItems_wooden(ArrayList<String> itemList){
        items_wooden = itemList;
        listAdapter_wooden = new InfoInputListAdapter_wooden(mView_wooden.getContext(), items_wooden);
        listLv_wooden.setAdapter(listAdapter_wooden);
        listLv_wooden.setOnItemClickListener((parent, view, position, id) -> {
            if (items_wooden ==null) return;
            String input = items_wooden.get(position);

            long endTime = System.currentTimeMillis();
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint(trackWord,input+"$$"+(endTime-startTime));

            inputTv_wooden.setText(input);
            listLayout_wooden.setVisibility(GONE);
        });

        listAdapter_wooden.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = listLv_wooden.getLayoutParams();
        if (itemHeight_wooden ==0) itemHeight_wooden = layoutParams.height;

        layoutParams.height = items_wooden.size() * itemHeight_wooden;
        listLv_wooden.setLayoutParams(layoutParams);

    }

    public void addItems_wooden(String[] itemArr){
        addItems_wooden(new ArrayList<>(Arrays.asList(itemArr)));
    }

}
